﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class showMsg : System.Web.UI.Page
{

    private string userName;
    int msgID = Convert.ToInt32(HttpContext.Current.Request.QueryString["ID"]);
    string strTableName = "MESSAGE" + Convert.ToString(DateTime.Now.Year);
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["userName"]!=null)
        {
            userName=Session["userName"].ToString();
            AddDialogBox();

        if (!IsPostBack)
        {

            GetMsg();
            BindDep();
        }
        }
        else
        {
            Response.Redirect(GetUrlPath.GetUrl + "/Login.aspx");
        }

    }
    //显示panel,读取用户名
    protected void Btntransmit_Click(object sender, EventArgs e)
    {
        
        panelTransmit.Visible = true;
        PanelWriteBakMsg.Visible = false;
        string strReadAllUers = "select * from USERS where USERNAME<>'" + userName + "' and USERNAME<>'admin' and DEPID=" + ddlDep.SelectedValue.ToString();
        DBOperation db = new DBOperation();
        SqlDataReader sdr = db.GetDataReader(strReadAllUers);
        ddlUsers.DataSource = sdr;
        ddlUsers.DataTextField = "TRUENAME";
        ddlUsers.DataValueField = "USERNAME";
        ddlUsers.DataBind();
        sdr.Close();
    }
    //绑定部门
    protected void BindDep()
    {
        string strFindDep = "select DEPID,DEPNAME from DEPARTMENT";
        DBOperation DB = new DBOperation();
        SqlDataReader sdr = DB.GetDataReader(strFindDep);
        ddlDep.DataSource = sdr;
        ddlDep.DataTextField = "DEPNAME";
        ddlDep.DataValueField = "DEPID";
        ddlDep.DataBind();
        sdr.Close();

    }
    //返回按钮事件
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/ReadMsg.aspx");
    }
    //获取url变量值，通过变量值读取信息
    protected void GetMsg()
    {

        //建立查询语句
        string strSearchMsg = "select * from " + strTableName + " where AUTOID=" + msgID;
        DBOperation db = new DBOperation();
        SqlDataReader sdr = db.GetDataReader(strSearchMsg);
        if (sdr.Read())
        {
            txtContent.Text = BasicClass.HtmlDiscode(sdr["CONTENT"].ToString());
            string userName = sdr["FROMUSERNAME"].ToString();
            string findTrueName = "select TRUENAME from USERS where USERNAME='"+userName+"'";
            DBOperation DB = new DBOperation();
            lblFromuserName.Text = Convert.ToString(db.ExcuteScalar(findTrueName));
            lblTitle.Text =BasicClass.HtmlDiscode(sdr["TITLE"].ToString());
            //查看后更改信息状态
            ChangeMsgStatus();
        }
        else
        {
            txtContent.Text = "信息无法读取，请联系管理员!";
        }
        sdr.Close();
    }
    //将已经查看的信息的查看状态更改为已经查看
    protected void ChangeMsgStatus()
    {
        string strChangeMsgStatus = "update " + strTableName + " set TOUSERSTATUS=1 where  AUTOID=" + msgID;
        DBOperation DB = new DBOperation();
        bool status = DB.ExcuteSql(strChangeMsgStatus);
    }
    //删除信息
    protected void btnDelMsg_Click(object sender, EventArgs e)
    {
        string strDelMsg = "delete from " + strTableName + " where AUTOID='" + msgID + "' and TOUSERSTATUS=1";
        DBOperation db = new DBOperation();
        if (db.ExcuteSql(strDelMsg))
        {
            Response.Write("<script language='javascript'>alert('删除成功！');location.href='ReadMsg.aspx';</script>");
            txtContent.Text = "";
            lblFromuserName.Text = "";
            lblTitle.Text = "";
        }
        else
        {
            Response.Write("<script language='javascript'>alert('删除失败！')</script>");
        }
    }
    //转发信息
    protected void btnSure_Click(object sender, EventArgs e)
    {
        messages msg = new messages();
        msg.FromUserName = userName;
        msg.ToUserName = ddlUsers.SelectedValue.ToString();
        msg.Title ="转发自"+lblFromuserName.Text+"的《"+BasicClass.HtmlEncode(lblTitle.Text)+"》" ;
        msg.Content =BasicClass.HtmlEncode(txtContent.Text);
        msg.SendDate = DateTime.Now;
        msg.ToUserStatus = 0;
        msg.FromUserStatus = 1;
        msg.TrueFlowID = "0";
        if (msg.AddMsg())
        {
            Response.Write("<script langeuage='javascript'>alert('转发成功！')</script>");

        }
        else
        {
            Response.Write("<script langeuage='javascript'>alert('转发失败！')</script>");
        }

    }
    //添加删除、转发等事件的确认对话框
    public void AddDialogBox()
    {
        btnDelMsg.Attributes.Add("onClick","return confirm('您确定要删除该信息吗？');");
        btnSure.Attributes.Add("onClick","return confirm('您确定要转发该信息吗？');");
    }
    protected void BtnWriteBak_Click(object sender, EventArgs e)
    {
        PanelWriteBakMsg.Visible = true;
        panelTransmit.Visible = false;
    }
    //回复信息
    protected void btnWriteBakMsg_Click(object sender, EventArgs e)
    {
        messages msg = new messages();
       
        msg.FromUserName = userName;
        string strSearchMsg = "select FROMUSERNAME from " + strTableName + " where AUTOID=" + msgID;
        DBOperation db = new DBOperation();
        msg.ToUserName =Convert.ToString( db.ExcuteScalar(strSearchMsg) );
        msg.Title ="回复您的"+"<<"+BasicClass.HtmlEncode(lblTitle.Text)+">>";
        msg.Content =BasicClass.HtmlEncode(txtWriteBakMsg.Text);
        msg.SendDate = DateTime.Now;
        msg.ToUserStatus = 0;
        msg.FromUserStatus = 1;
        msg.TrueFlowID = "0";
        if (msg.AddMsg())
        {
            Response.Write("<script langeuage='javascript'>alert('回复成功！')</script>");
            txtWriteBakMsg.Text = "";

        }
        else
        {
            Response.Write("<script langeuage='javascript'>alert('信息回复失败！')</script>");
        }
    }
    protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e)
    {
        Btntransmit_Click(sender,e);
    }
}
